﻿using FOModels.HomeEquityLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.HomeEquityLoan
{
    public class HomeEquityLoanDAL
    {
        private SQLDataAccess db;
        public HomeEquityLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from HomeEquityLoan order by HomeEquityLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoanrelati(int CompanyID, int HomeEquityLoanID, int langid)
        {
            return db.exeSelect("select * from HomeEquityLoan ca join HomeEquityLoanLang hl on ca.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.HomeEquityLoanID != " + HomeEquityLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableHomeEquityLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableHomeEquityLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getHomeEquityLoanDefault(int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableHomeEquityLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoanDefaultTest(int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableHomeEquityLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from HomeEquityLoan sa join HomeEquityLoanLang hl on sa.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableHomeEquityLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getHomeEquityLoanByID(int HomeEquityLoanID, int langid)
        {
            return db.exeSelect("select * from HomeEquityLoan ca join HomeEquityLoanLang hl on ca.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.HomeEquityLoanID = " + HomeEquityLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getHomeEquityLoanByIDManager(int HomeEquityLoanID, int langid)
        {
            return db.exeSelect("select * from HomeEquityLoan ca join HomeEquityLoanLang hl on ca.HomeEquityLoanID = hl.HomeEquityLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.HomeEquityLoanID = " + HomeEquityLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateHomeEquityLoan(HomeEquityLoanModels models)
        {
            return db.exeUpdate("update HomeEquityLoan set HomeEquityLoanName = N'" + models.HomeEquityLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where HomeEquityLoanID = " + models.HomeEquityLoanID + "");
        }
        public int InsertHomeEquityLoan(HomeEquityLoanModels models)
        {
            return db.exeUpdate("insert into HomeEquityLoan values (N'" + models.HomeEquityLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteHomeEquityLoan(int HomeEquityLoanID)
        {
            return db.exeUpdate("Delete from HomeEquityLoan where HomeEquityLoanID = " + HomeEquityLoanID + "");
        }
        public int UpdateHomeEquityLoanLang(HomeEquityLoanModels models, int langid)
        {
            return db.exeUpdate("update HomeEquityLoanLang set HomeEquityLoanName = N'" + models.HomeEquityLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where HomeEquityLoanID = " + models.HomeEquityLoanID + " and LangID=" + langid + "");
        }
        public int InsertHomeEquityLoanLang(HomeEquityLoanModels models, int langid)
        {
            return db.exeUpdate("insert into HomeEquityLoanLang values (" + models.HomeEquityLoanID + "," + langid + ",N'" + models.HomeEquityLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteHomeEquityLoanLang(int HomeEquityLoanID)
        {
            return db.exeUpdate("Delete from HomeEquityLoanLang where HomeEquityLoanID = " + HomeEquityLoanID + "");
        }

        public int DeleteHomeEquityLoanApply(int HomeEquityLoanApplyID)
        {
            return db.exeUpdate("Delete from HomeEquityLoanApply where HomeEquityLoanApplyID = " + HomeEquityLoanApplyID + "");
        }
        public DataTable getcontractbyid(int HomeEquityLoanapplyid)
        {
            return db.exeSelect("select * from HomeEquityLoanApply ca join [HomeEquityLoan] c on ca.HomeEquityLoanID = c.HomeEquityLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.HomeEquityLoanApplyID = " + HomeEquityLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from HomeEquityLoanApply ca join [HomeEquityLoan] c on ca.HomeEquityLoanID = c.HomeEquityLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from HomeEquityLoanApply ca join [HomeEquityLoan] c on ca.HomeEquityLoanID = c.HomeEquityLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int HomeEquityLoanApply(int HomeEquityLoanID, HomeEquityLoanApplyModels app)
        {
            return db.exeUpdate("insert into HomeEquityLoanApply(HomeEquityLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + HomeEquityLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(HomeEquityLoanApplyModels HomeEquityLoan)
        {
            return db.exeUpdate("update HomeEquityLoanApply set StatusID= " + HomeEquityLoan.StatusID + " where HomeEquityLoanApplyID = " + HomeEquityLoan.HomeEquityLoanApplyID + "");
        }
    }
}
